package se19team1e.gui.awt;

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import se19team1e.gui.MessageBoxHelper;
import se19team1e.util.StringUtil;

/**
 * 
 * A GUI panel for showing the login input fields.
 * 
 * <p>
 * Contains both the username and password input fields.
 * </p>
 * 
 * @author lai shiaw san
 *
 */
public class LoginInputPanel extends Panel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * An awt text field to enter username.
     */
    private final TextField usernameField;
    
    /**
     * An awt text field to enter password.
     */
    private final TextField passwordField;

    /**
     * Constructs the login input panel.
     */
    public LoginInputPanel() {
        
        final int USERNAME_INPUTWIDTH = 20;
        final int PASSWORD_INPUTWIDTH = 20;
        
        this.setLayout( new GridLayout( 0, 2 ) );
        
        // create and add the username field
        this.usernameField = new TextField( USERNAME_INPUTWIDTH );
        this.add( new Label( "Username" ) );
        this.add( this.usernameField );

        // create and add the password field
        this.passwordField = new TextField( PASSWORD_INPUTWIDTH );
        this.passwordField.setEchoChar( '*' );
        this.add( new Label( "Password" ) );
        this.add( this.passwordField );
    }
    
    /**
     * Clears any previous input.
     */
    public void initialize() { 
        this.usernameField.setText( "" );
        this.passwordField.setText( "" );
    }

    /**
     * Validates the login credentials input to see if it is valid for login.
     * 
     * <p>
     * Returns false if validation fails.
     * </p>
     * <p>
     * Displays an error message if it fails, using a message box.
     * </p>
     * @param messageBoxHelper Helper to create message boxes.
     * @return True if valid.
     */
    public boolean validateInput( MessageBoxHelper messageBoxHelper ) {
        // check for empty entry
        if ( StringUtil.isEmptyOrNull( getUsernameInput() ) ) {
            // failure, display error message
            messageBoxHelper.displayMissingInputErrorMessage( "username" );
            
            return false; 
        }
        if ( StringUtil.isEmptyOrNull( getPasswordInput() ) ) {
            // failure, display error message
            messageBoxHelper.displayMissingInputErrorMessage( "password" );
            
            return false; 
        }
        
        return true;
    }

    /**
     * Get the username in the input box.
     * @return Value in input box.
     */
    public String getUsernameInput() {
        return this.usernameField.getText();
    }

    /**
     * Get the password in the input box.
     * @return Value in input box.
     */
    public String getPasswordInput() {
        return this.passwordField.getText();
    }

}
